package top.lv666.graduation_design_serve.service;

import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lv666.graduation_design_serve.mapper.MessageMapper;
import top.lv666.graduation_design_serve.pojo.Blog;
import top.lv666.graduation_design_serve.pojo.Message;
import top.lv666.graduation_design_serve.pojo.ReplyMessage;
import top.lv666.graduation_design_serve.util.Tools;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MessageServiceImpl implements MessageService {
    private final Map<String, Object> paramMap = new HashMap<>();

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private BlogServiceImpl blogService;

    /**
     * @apiNote 在博客中发表评论
     */
    @Override
    public void CreateBlogMessage(Map<String, Object> map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        map.remove("request");
        // 顺利通过不报错即为验证成功！
        Tools.isMapNull(map);
        // 顺利通过不报错即为验证成功！
        Tools.isLogin(request);
        // 获取博客ID/详细信息
        String blogId = (String) map.get("blogId");
        Blog blog = blogService.selectBlogInfo(blogId);
        if (blog == null) {
            throw new Error("非法ID");
        }
        // 获取用户ID
        String userId = Tools.getUserId(request);
        map.put("userId", userId);
        // 获取系统时间
        map.put("createTime", Tools.getTime());
        // emoji字符转义
        String str = EmojiParser.parseToAliases((String) map.get("messageContent"));
        map.put("messageContent", str);
        // 更新Blog信息
        paramMap.clear();
        paramMap.put("blogID", blogId);
        paramMap.put("commNumber", blog.getCommNumber() + 1);
        paramMap.put("request", request);
        blogService.UpdateBlog(paramMap);
        // 进行数据库操作
        messageMapper.CreateBlogMessage(map);
    }

    /**
     * @apiNote 获取评论
     */
    @Override
    public List<Message> QueryBlogMessage(Map<String, Object> map) {
        try {
            Tools.isMapNull(map);
            int getPage = (int) map.get("getPage");
            getPage = (getPage - 1) * (int) map.get("getNumber");
            map.put("getPage", getPage);

            List<Message> info = messageMapper.QueryBlogMessage(map);
            for (Message message : info) {
                message.setMessageContent(EmojiParser.parseToUnicode(message.getMessageContent()));
            }
            return info;
        } catch (Exception | Error e) {
            throw e;
        }
    }

    /**
     * @apiNote 获取当前博客的评论总数
     */
    @Override
    public int getBlogMessageNumber(String blogId) {
        try {
            if (blogId.equals("")) {
                throw new Error("请求参数错误！");
            }
            return messageMapper.getBlogMessageCount(blogId);
        } catch (Exception | Error e) {
            throw e;
        }
    }

    /**
     * @apiNote 发布二级评论
     */
    @Override
    public void CreateSecondaryMessage(Map<String, Object> map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        map.remove("request");
        // 获取一级回复的ID
        int messageId = (int) map.get("messageId");
        // 查询该ID是否存在
        Message message = messageMapper.queryBlogMessageById(messageId);
        if (message == null) {
            throw new Error("ID数据异常");
        }
        // 获取当前点赞数
        int replyNumber = message.getReplyNumber();
        // 判空
        Tools.isMapNull(map);
        // 判断登录状态
        Tools.isLogin(request);
        // 获取操作用户ID
        String userId = Tools.getUserId(request);
        map.put("userId", userId);
        // emoji字符转义
        String str = EmojiParser.parseToAliases((String) map.get("replyContent"));
        map.put("replyContent", str);
        // 获取当前时间
        String time = Tools.getTime();
        map.put("createTime", time);
        System.out.println(map);
        messageMapper.setSecondaryMessage(map);
        paramMap.clear();
        // 更改一级评论下的回复数量
        paramMap.put("messageId", map.get("messageId"));
        paramMap.put("replyNumber", replyNumber + 1);
        messageMapper.modifyBlogMessage(paramMap);
    }

    /**
     * @apiNote 获取二级评论下的回复数量
     */
    @Override
    public int QuerySecondaryMessageCount(Map<String, Object> map) {
        int messageId = (int) map.get("messageId");
        return messageMapper.queryReplyMessageCount(messageId);
    }

    /**
     * @apiNote 分页获取二级回复
     */
    @Override
    public List<ReplyMessage> QuerySecondaryMessageLimit(Map<String, Object> map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        map.remove("request");
        Tools.isMapNull(map);
        // 查询数据量更改
        int getPage = (int) map.get("getPage");
        getPage = (getPage - 1) * (int) map.get("getNumber");
        map.put("getPage", getPage);
        // emoji表情转码
        List<ReplyMessage> replyMessageList = messageMapper.queryReplyMessage(map);
        for (ReplyMessage r : replyMessageList) {
            r.setReplyContent(EmojiParser.parseToUnicode(r.getReplyContent()));
        }
        return messageMapper.queryReplyMessage(map);
    }
}
